package prog.view;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import prog.model.Project;
/*
 * ProjectPanel.java
 *
 * Created on Jan 8, 2011, 11:06:22 PM
 */

/**
 *
 * @author MrKhoai
 */
public class ProjectPanel extends javax.swing.JPanel {
    private ButtonGroup group = null;
    private ButtonGroup groupOps = null;
    /** Creates new form ProjectPanel */
    public ProjectPanel() {
        initComponents();
        addListenner();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = jPanel3 = new javax.swing.JPanel() {
            @Override
            public void paint(Graphics g) {
                Graphics2D graph=(Graphics2D)g;
                Image img = Toolkit.getDefaultToolkit().getImage("resource/tool/projectproperty.jpg");
                graph.drawImage(img, 0, 0, this);
                super.paint(g);
            }
        };
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_projectName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txt_url = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        sp_lvLimit = new javax.swing.JSpinner();
        sp_lvLimit.setValue(1);
        jLabel5 = new javax.swing.JLabel();
        txt_partern = new javax.swing.JTextField();
        rb_op1 = new javax.swing.JRadioButton();
        rb_op2 = new javax.swing.JRadioButton();
        rb_op3 = new javax.swing.JRadioButton();
        group = new ButtonGroup();
        group.add(rb_op1);
        group.add(rb_op2);
        group.add(rb_op3);
        jLabel6 = new javax.swing.JLabel();
        txt_save = new javax.swing.JTextField();
        bt_fileChoose = new javax.swing.JButton();
        rb_ensureDown = new javax.swing.JRadioButton();
        rb_ignoreDown = new javax.swing.JRadioButton();

        setPreferredSize(new java.awt.Dimension(460, 394));

        jPanel3.setOpaque(false);
        jPanel3.setPreferredSize(new java.awt.Dimension(460, 394));

        jLabel1.setBackground(new java.awt.Color(51, 204, 255));
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Tên dự án và mức tải về");
        jLabel1.setOpaque(true);

        jLabel2.setText("Tên dự án:");

        txt_projectName.setText("http://");

        jLabel3.setText("URL:");

        txt_url.setText("http://");

        jLabel4.setText("Mức tải về:");

        jLabel5.setBackground(new java.awt.Color(102, 102, 255));
        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("Các lựa chọn tải về");
        jLabel5.setOpaque(true);

        txt_partern.setEnabled(false);

        rb_op1.setSelected(true);
        rb_op1.setText("Tải tất cả");
        rb_op1.setActionCommand("downAll");
        rb_op1.setOpaque(false);

        rb_op2.setText("Chỉ tải những file trong file filter");
        rb_op2.setActionCommand("onlyFileFilter");
        rb_op2.setEnabled(false);
        rb_op2.setOpaque(false);

        rb_op3.setText("Tải theo partern");
        rb_op3.setActionCommand("usingPartern");
        rb_op3.setOpaque(false);

        jLabel6.setText("Lưu xuống:");

        txt_save.setEditable(false);
        txt_save.setText("C:\\download");

        bt_fileChoose.setText("...");
        bt_fileChoose.setActionCommand("chooseFile");
        bt_fileChoose.setOpaque(false);

        rb_ensureDown.setSelected(true);
        rb_ensureDown.setText("Tải");
        rb_ensureDown.setActionCommand("ensureDown");
        rb_ensureDown.setEnabled(false);
        rb_ensureDown.setOpaque(false);

        rb_ignoreDown.setText("Không tải");
        rb_ignoreDown.setActionCommand("ignoreDown");
        rb_ignoreDown.setEnabled(false);
        rb_ignoreDown.setOpaque(false);
        groupOps = new ButtonGroup();
        groupOps.add(rb_ensureDown);
        groupOps.add(rb_ignoreDown);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(sp_lvLimit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txt_url, javax.swing.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
                                    .addComponent(txt_projectName)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                        .addComponent(txt_save, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(bt_fileChoose, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rb_op1)
                            .addComponent(txt_partern)
                            .addComponent(rb_op2)
                            .addComponent(rb_op3))))
                .addContainerGap(71, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(176, Short.MAX_VALUE)
                .addComponent(rb_ensureDown)
                .addGap(18, 18, 18)
                .addComponent(rb_ignoreDown)
                .addGap(156, 156, 156))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_projectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(txt_url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txt_save, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(sp_lvLimit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(bt_fileChoose))
                .addGap(35, 35, 35)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addComponent(rb_op1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rb_op2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rb_op3)
                .addGap(2, 2, 2)
                .addComponent(txt_partern, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rb_ensureDown)
                    .addComponent(rb_ignoreDown))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 460, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 394, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void addListenner() {
        MyListener listenner = new MyListener();
        rb_op1.addActionListener(listenner);
        rb_op2.addActionListener(listenner);
        rb_op3.addActionListener(listenner);
        sp_lvLimit.addChangeListener(listenner);
        bt_fileChoose.addActionListener(listenner);
    }

    public Project getProject() {
        Project pj = new Project();

        pj.setName(txt_projectName.getText());
        pj.setURL(txt_url.getText());
        
        String dst = txt_save.getText();
        dst += '\\';
        pj.setDst(dst);
        
        pj.setLv((Integer)sp_lvLimit.getValue());
        String actionCommand = group.getSelection().getActionCommand();
        if(actionCommand.equals("downAll")) {
            pj.setInsOp(1);
        }
        if(actionCommand.equals("onlyFileFilter")) {
            pj.setInsOp(2);
        }
        if(actionCommand.equals("usingPartern")) {
            pj.setInsOp(3);
            pj.setPartern(txt_partern.getText());
            String actCommand = groupOps.getSelection().getActionCommand();
            if(actCommand.equals("ensureDown"))
                pj.setdownOp(1);
            else
                pj.setdownOp(2);
        }
        return pj;
    }

    public void clear() {
        txt_projectName.setText("http://");
        txt_url.setText("http://");
        txt_save.setText("C:\\download");
        sp_lvLimit.setValue(1);
        rb_op1.setSelected(true);
        txt_partern.setText(null);
        rb_ensureDown.setEnabled(false);
        rb_ignoreDown.setEnabled(false);
    }

    public void load() {
        String s = null;
        try {
            Toolkit kit = Toolkit.getDefaultToolkit();
            Clipboard clipboard = kit.getSystemClipboard();
            Transferable trans = clipboard.getContents(null);
            s = (String)trans.getTransferData(DataFlavor.stringFlavor);
        } catch (Exception e) {

        }
        if(s != null && s.startsWith("http://")) {
            txt_projectName.setText(s);
            txt_url.setText(s);
        }
        else {
            
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_fileChoose;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton rb_ensureDown;
    private javax.swing.JRadioButton rb_ignoreDown;
    private javax.swing.JRadioButton rb_op1;
    private javax.swing.JRadioButton rb_op2;
    private javax.swing.JRadioButton rb_op3;
    private javax.swing.JSpinner sp_lvLimit;
    private javax.swing.JTextField txt_partern;
    private javax.swing.JTextField txt_projectName;
    private javax.swing.JTextField txt_save;
    private javax.swing.JTextField txt_url;
    // End of variables declaration//GEN-END:variables

    class MyListener implements ActionListener, ChangeListener {

        public void actionPerformed(ActionEvent e) {
            String actionCommand = e.getActionCommand();
            if(actionCommand.equals("usingPartern")) {
                txt_partern.setEnabled(true);
                rb_ensureDown.setEnabled(true);
                rb_ignoreDown.setEnabled(true);
            }
            if(actionCommand.equals("downAll")) {
                txt_partern.setEnabled(false);
                rb_ensureDown.setEnabled(false);
                rb_ignoreDown.setEnabled(false);
            }
            if(actionCommand.equals("onlyFileFilter")) {
                txt_partern.setEnabled(false);
                rb_ensureDown.setEnabled(false);
                rb_ignoreDown.setEnabled(false);
            }
            if(actionCommand.equals("chooseFile")) {
                JFileChooser fc = new JFileChooser();
                fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                fc.showSaveDialog(bt_fileChoose);
                File f = fc.getSelectedFile();
                if(f != null)
                    txt_save.setText(f.getPath());
            }
        }

        public void stateChanged(ChangeEvent e) {
            JSpinner sp = (JSpinner)e.getSource();
            if((Integer)sp.getPreviousValue() < 0)
                sp.setValue(0);
        }
    }
}
